package com.cn.wanxi.control;/**
 * @BelongsProject: online
 * @BelongsPackage: com.cn.wanxi.control
 * @Author: lld
 * @CreateTime: 2020-03-11 08:49
 * @Description: control
 */

import com.cn.wanxi.entity.QuestionsPaperEntity;
import com.cn.wanxi.service.QuestionsPaperService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *@ClassName Test_QuestionsController
 *@Author lld
 *@Date 2020/3/11 8:49
 *@Version 1.0
 **/
@Controller
@RequestMapping("/questionPaper")
public class QuestionsPaperController {

    @Autowired
    private QuestionsPaperService questions_paperService;

    @RequestMapping("/findAll")
    @ResponseBody
    public void findAll(HttpServletRequest request, HttpServletResponse response, QuestionsPaperEntity questions_paperEntity) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        List<QuestionsPaperEntity> list = questions_paperService.findAll(questions_paperEntity);
        JSONObject json = new JSONObject();
        json.put("result",list);
        response.getWriter().write(json.toString());
    }
    @RequestMapping("/addTitle")
    @ResponseBody
    public void add(HttpServletResponse response,HttpServletRequest request,QuestionsPaperEntity questionsPaperEntity) throws IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        questions_paperService.add(questionsPaperEntity);
    }
    @RequestMapping("delete")
    @ResponseBody
    public void delete(HttpServletRequest request, HttpServletResponse response, QuestionsPaperEntity questions_paperEntity) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int resultId = questions_paperService.del(questions_paperEntity);
        String result = resultId == 1 ? "删除成功":"删除失败";
        JSONObject json = new JSONObject();
        json.put("result",result);
        response.getWriter().write(json.toString());
    }
    @RequestMapping("/update")
    @ResponseBody
    public void update(HttpServletResponse response, HttpServletRequest request, QuestionsPaperEntity questions_paperEntity) throws IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        int result = questions_paperService.update(questions_paperEntity);
        JSONObject json = new JSONObject();
        json.put("result",result);
        response.getWriter().write(json.toString());
    }
    @RequestMapping("getById")
    @ResponseBody
    public List<QuestionsPaperEntity> getById(HttpServletRequest request, HttpServletResponse response, QuestionsPaperEntity questions_paperEntity) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        List<QuestionsPaperEntity> list = questions_paperService.getById(questions_paperEntity);
        JSONObject json = new JSONObject();
        json.put("result",list);
        response.getWriter().write(json.toString());
        return list;
    }
}
